package com.tech.koufu.ui.activity.login;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.tech.koufu.R;
import com.tech.koufu.tools.PublicFuc;
import com.tech.koufu.tools.Statics;

import android.app.Activity;
import android.app.DownloadManager.Request;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity{

	private TextView tv_title,tv_righttitle;
	private EditText ed_username,ed_useridname,
	ed_userpwd,ed_userpwdagain,ed_usermob,ed_usermobpwd;
	private Button btn_sendmobpwd,btn_toregister;
	private RequestQueue mQueue;
	private StringRequest stringRequest;
	private LinearLayout ll_read;
	private ImageView img_register_check;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initView();
	}

	private void initView(){ 
		myOnClickListener myClick = new myOnClickListener();
		ImageView img_callback = (ImageView) findViewById(R.id.img_callback);
		img_callback.setVisibility(View.VISIBLE);
		img_callback.setOnClickListener(myClick);
		tv_title = (TextView)findViewById(R.id.tv_title);
		tv_title.setText("注册");
		tv_righttitle = (TextView)findViewById(R.id.tv_righttitle);
		tv_righttitle.setVisibility(View.VISIBLE);
		tv_righttitle.setText("登录");
		tv_righttitle.setOnClickListener(myClick);
		ed_username = (EditText)findViewById(R.id.ed_username);
		ed_useridname = (EditText)findViewById(R.id.ed_useridname);
		ed_userpwd = (EditText)findViewById(R.id.ed_userpwd);
		ed_userpwdagain = (EditText)findViewById(R.id.ed_userpwdagain);
		ed_usermob = (EditText)findViewById(R.id.ed_usermob);
		ed_usermobpwd = (EditText)findViewById(R.id.ed_usermobpwd);
		btn_sendmobpwd = (Button)findViewById(R.id.btn_sendmobpwd);
		btn_sendmobpwd.setOnClickListener(myClick);
		btn_toregister = (Button)findViewById(R.id.btn_toregister);
		btn_toregister.setOnClickListener(myClick);
		mQueue = Volley.newRequestQueue(getApplicationContext());
		ll_read = (LinearLayout)findViewById(R.id.ll_read);
		ll_read.setOnClickListener(myClick);
		img_register_check = (ImageView) findViewById(R.id.img_register_check);
	}

	private class myOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tv_righttitle:
				Intent tologinintent = new Intent(RegisterActivity.this,LoginActivity.class);
				startActivityForResult(tologinintent, PublicFuc.code_login_suss);//(tologinintent);
				RegisterActivity.this.finish();
				break;
			case R.id.btn_toregister:
				if(ed_username.getText().toString().equals("") || ed_useridname.getText().toString().equals("") 
						||ed_userpwd.getText().toString().equals("") ||ed_userpwdagain.getText().toString().equals("")
						||ed_usermob.getText().toString().equals("") ||ed_usermobpwd.getText().toString().equals("")){
					Toast.makeText(getApplicationContext(), "请补全用户信息！", Toast.LENGTH_SHORT).show();
				}else if(!ed_userpwd.getText().toString().equals("") &&!ed_userpwdagain.getText().toString().equals("") 
						&&!ed_userpwdagain.getText().toString().equals(ed_userpwd.getText().toString())){
					Toast.makeText(getApplicationContext(), "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
				}else {
					toRegister();
				}
//				Intent loginintent = new Intent(RegisterActivity.this,LoginActivity.class);
//				startActivity(loginintent);
//				RegisterActivity.this.finish();
				break;
			case R.id.btn_sendmobpwd:
				String mobpwd  = ed_usermob.getText().toString();
				if(!mobpwd.equals("") && mobpwd.length() == 11){
					getMobPwd();
				}else{
					Toast.makeText(getApplicationContext(), "请正确填写手机号码!", Toast.LENGTH_SHORT).show();
				}				
				break;
			case R.id.ll_read:
				String tag = img_register_check.getTag().toString();
				if(tag.equals("0")){
					img_register_check.setTag("1");
					img_register_check.setImageResource(R.drawable.register_check);
				}else{
					img_register_check.setTag("0");
					img_register_check.setImageResource(R.drawable.register_uncheck);
				}
				break;
			case R.id.img_callback:
				RegisterActivity.this.finish();
				break;
			default:
				break;
			}
		}		
	}

	private void getMobPwd(){
		String url = Statics.URL_PHP + "sendMessage";

		stringRequest = new StringRequest(Method.POST, url, new Response.Listener<String>() {

			@Override
			public void onResponse(String response) {
				// TODO Auto-generated method stub
				if(response != null && !response.equals("")){
					Log.v("RegisterMob!-!", response);
					JSONObject obj;
					try {
						obj = new JSONObject(response);
						String status = obj.getString("status");
						if(status.equals("0")){
							Toast.makeText(getApplicationContext(), "验证码发送成功!", Toast.LENGTH_SHORT).show();
						}else{
							Toast.makeText(getApplicationContext(), "发送失败，请重新发送!", Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}					
				}
			}
		}, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub

			}
		}){

			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				// TODO Auto-generated method stub
				Map<String, String> params = new HashMap<String, String>();
				params.put("mobile", ed_usermob.getText().toString());
				return params;
			}

		};
		mQueue.add(stringRequest);
	}
	
	private void toRegister(){
		String url = Statics.URL_PHP + "register";
		
		stringRequest = new StringRequest(Method.POST, url, new Response.Listener<String>() {

			@Override
			public void onResponse(String response) {
				Log.v("RegisterBtn", response);
				try {
					JSONObject obj = new JSONObject(response);
					if(obj.getInt("status") == 0){
						Toast.makeText(getApplicationContext(), "恭喜您，注册成功", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
						startActivityForResult(intent, PublicFuc.code_login_suss);
						RegisterActivity.this.finish();
					}else{
						String info = obj.getString("info");
						Toast.makeText(getApplicationContext(),info, Toast.LENGTH_SHORT).show();
					}

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				
			}
		}){

			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> params = new HashMap<String, String>();
				params.put("userName", ed_username.getText().toString());
				params.put("nick", ed_useridname.getText().toString());
				params.put("password", ed_userpwd.getText().toString());
				params.put("mobile", ed_usermob.getText().toString());
				params.put("checkCode", ed_usermobpwd.getText().toString());
				return params;
			}			
		};
		mQueue.add(stringRequest);
	}
}
